package org.shan.security.core.social;

import org.shan.security.core.social.mlnx.connet.MlnxConnectionFactory;
import org.shan.security.core.social.qq.connet.QQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurer;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.AuthenticationNameUserIdSource;
import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * Created by amanda.shan on 2019/3/19.
 */
@Configuration
@EnableSocial
public class SocialConfig extends SpringSocialConfigurer implements SocialConfigurer {

    @Autowired
    private DataSource dataSource;

    @Override
    protected <T> T postProcess(T object) {
        SocialAuthenticationFilter filter = (SocialAuthenticationFilter) super.postProcess(object);
        // 配置SocialAuthenticationFilter拦截的url前缀
        filter.setFilterProcessesUrl("/auth");
        filter.setSignupUrl("/user/signup");
        return (T) filter;
    }


    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment
            environment) {
        // qq
        connectionFactoryConfigurer.addConnectionFactory(new QQConnectionFactory("qq", "101563274",
                "8a680d93644259bfbbbcd8cd4ae231c5"));

        // mlnx平台
        connectionFactoryConfigurer.addConnectionFactory(new MlnxConnectionFactory("mlnx", "client1", "123456"));
    }

    @Override
    public UserIdSource getUserIdSource() {
        return new AuthenticationNameUserIdSource();
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {

        // 内存中存储
//        InMemoryUsersConnectionRepository repository =
//                new InMemoryUsersConnectionRepository(connectionFactoryLocator);
//        repository.setConnectionSignUp(new ConnectionSignUp() {
//            @Override
//            public String execute(Connection<?> connection) {
//
//                return connection.getKey().getProviderUserId();
//            }
//        });

        // 数据库存储
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
                connectionFactoryLocator, Encryptors.noOpText());
        // 指定表前缀，后缀是固定的，在JdbcUsersConnectionRepository所在位置
        // repository.setTablePrefix("imooc_");

        repository.setConnectionSignUp(new ConnectionSignUp() {
            @Override
            public String execute(Connection<?> connection) {

                return connection.getKey().getProviderUserId();
            }
        });

        return repository;
    }

    /**
     * 用来处理注册流程的工具类
     *
     * @param connectionFactoryLocator
     * @return
     */
    @Bean
    public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator) {
        return new ProviderSignInUtils(connectionFactoryLocator,
                getUsersConnectionRepository(connectionFactoryLocator)) {
        };
    }
}
